{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bar\n",
      "foo\n"
     ]
    }
   ],
   "source": [
    "## 1、python中函数的工作原理\n",
    "\n",
    "import inspect\n",
    "frame = None\n",
    "def foo():\n",
    "    bar()\n",
    "def bar():\n",
    "    global frame\n",
    "    frame = inspect.currentframe()\n",
    "\n",
    "#############################################################\n",
    "## python.exe 会用一个叫做 PyEval_EvalFramEx（c函数）去执行foo函数，首先会创建一个栈帧（stack frame）\n",
    "## python 一切皆对象，栈帧也是对象，字节码对象\n",
    "## 当foo调用子函数bar，又会创建一个栈帧\n",
    "## 所有的栈帧都是分配在堆内存上，这就决定了栈帧可以独立于调用者存在\n",
    "#############################################################\n",
    "\n",
    "# import dis\n",
    "# print(dis.dis(foo))\n",
    "\n",
    "foo()\n",
    "print(frame.f_code.co_name)\n",
    "caller_frame = frame.f_back\n",
    "print(caller_frame.f_code.co_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  2           0 LOAD_CONST               1 (1)\n",
      "              2 YIELD_VALUE\n",
      "              4 POP_TOP\n",
      "\n",
      "  3           6 LOAD_CONST               2 ('bobby')\n",
      "              8 STORE_FAST               0 (name)\n",
      "\n",
      "  4          10 LOAD_CONST               3 (2)\n",
      "             12 YIELD_VALUE\n",
      "             14 POP_TOP\n",
      "\n",
      "  5          16 LOAD_CONST               4 (30)\n",
      "             18 STORE_FAST               1 (age)\n",
      "\n",
      "  6          20 LOAD_CONST               5 ('imooc')\n",
      "             22 RETURN_VALUE\n",
      "None\n",
      "-1\n",
      "{}\n",
      "2\n",
      "{}\n",
      "12\n",
      "{'name': 'bobby'}\n"
     ]
    }
   ],
   "source": [
    "def gen_func():\n",
    "    yield 1\n",
    "    name = \"bobby\"\n",
    "    yield 2\n",
    "    age = 30\n",
    "    return \"imooc\"\n",
    "\n",
    "import dis\n",
    "gen = gen_func()\n",
    "print(dis.dis(gen))\n",
    "\n",
    "print(gen.gi_frame.f_lasti)\n",
    "print(gen.gi_frame.f_locals)\n",
    "next(gen)\n",
    "print(gen.gi_frame.f_lasti)\n",
    "print(gen.gi_frame.f_locals)\n",
    "next(gen)\n",
    "print(gen.gi_frame.f_lasti)\n",
    "print(gen.gi_frame.f_locals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
